package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qf.dto.UserDTO;
import com.qf.entity.User;
import com.qf.exception.JxcException;
import com.qf.mapper.UserMapper;
import com.qf.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.Size;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public UserDTO login(String username, String password) {
        //1. 根据用户名和密码查询用户信息.
        User param = new User();
        param.setUsername(username);
        param.setPassword(password);

        //2. 执行查询
        User user = userMapper.selectOne(param);

        //3. 登录失败 - 抛出异常
        if(user == null){
            // 用户名或密码错误
            log.info("【用户登录】 您的用户名或密码错误 username = {},password = {}",username,password);
            throw new JxcException(250,"您的用户名或密码错误,请重新登录!!");
        }
        //4. 登录成功 - 封装数据
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);

        return userDTO;
    }
}
